<%@ page import="cleanstarxchange.server.*"%>
<%@ page import="java.util.ArrayList"%>
 <%@ page import="java.text.DecimalFormat"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<%	//getting the user id from previous page	
	Integer uid = Integer.parseInt( request.getParameter( "uid" ) );
 %>

	<script language="javascript">
	<!--
	function moveToHome()
	{
   		var frm = document.frm;
	   	frm.action = "index.jsp?uid=<%=uid%>";
   		frm.submit();
	}

	function moveToUpdate()
	{
   		var frm = document.frm;
	   	frm.action = "update.jsp?uid=<%=uid%>&updateid=1";
   		frm.submit();
	}
	//-->
	</script>
	
	
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Star XChange</title>
		<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
		<style type="text/css"></style>
		<!--[if IE]>
		<style type="text/css"> 
		/* place css fixes for all versions of IE in this conditional comment */
		.twoColElsLtHdr #sidebar1 { padding-top: 30px; }
		.twoColElsLtHdr #mainContent { zoom: 1; padding-top: 15px; }
		/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
		</style>
		<![endif]-->
	</head>



	<body class="twoColElsLtHdr">
	
	<%
		//initializing datastore object for connection to datastore
		DataStore ds = new DataStore();
		//getting the user object of the current user from datastore
		User user = ds.getUser( new Long(uid) );
		//ds.computeAssetValue(user);
		//getting the owned stock list of the current user in arraylist
		ArrayList<OwnedStock> owned = ds.getOwnedStocks( new Long(uid));
		//getting the cash amount of the current user
		double balance = user.getBalance();
		//getting the total assetvalue of the current user
		double assetValue = user.getAssetValue();
		//formatting balance and assetValue for easier reading
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		balance = Double.valueOf(twoDForm.format(balance));
		assetValue = Double.valueOf(twoDForm.format(assetValue));
	%>
	
	<!-- container wraps the whole contents of the webpage -->
	<div id="container">
  		<!-- header contains the logo of our application which links to index.jsp page -->
  		<div id="header"><a href="index.jsp?uid=<%=uid%>"><img src="media/logo.jpg"/></a>
  		<!-- end #header -->
  		</div>
  		<!-- sidebar contains the menu to navigate the application -->
  		<div id="sidebar1">
   			<ul class="sidenav">
				<li>
                	<a href="portfolio.jsp?uid=<%=uid%>" title="My Portfolio">My Portfolio
                    <span>See how rich I am</span>
                    </a>
                    
                </li>
			    <li>
                	<a href="stocklist.jsp?uid=<%=uid%>" title="Stocks">Stocks
                    <span>See the market flow</span>
                    </a>
                </li>
				<li>
                	<a href="rank.jsp?uid=<%=uid%>" title="Rank">Rank
                    <span>Who is the winner now</span>
                    </a>
                </li>
				<li>
                	<a href="howtoplay.jsp?uid=<%=uid%>" title="HowToPlay">How To Play
                    <span>Learn how to play the game</span>
                    </a>
                </li>
			</ul>
    	<!-- end #sidebar1 -->
    	</div>
  	    <!-- mainContent contains the personal financial information of the current user -->
  		<div id="mainContent">
    		<!-- greeting msg, name, balance, total asset value of the current user -->
    		<div id="uinf">
    			Welcome to your portfolio page <%=user.getFirstName()%> <%=user.getLastName()%>. You have $<%=balance%>.<br/>
    			The total value of your assets is $<%=assetValue %>.
    		</div>

			<!-- showing the update button for developers 
				 this will be removed after finishing automating update
			 -->	
    		<form name="frm" method="post">
				<input type="button" name="btregister" value = "Home" onClick="moveToHome();">
				<% if ( uid==713865194 || uid==21014218 || uid==783920693 || uid==514766462 ){ %>
				<input type="button" name="btregister" value = "Update Now" onClick="moveToUpdate();">
				<%} %>
			</form>
			<!-- table for the owned stocks -->
			<div id="shortcut">
    			<table width="95%" border="0" cellspacing="2" cellpadding="2">
  					<tr>
  						<th scope="col">Sell</th>
    					<th scope="col">Celeb Name</th>
    					<th scope="col">Price bought</th>
    					<th scope="col">Shares</th>
    					<th scope="col">Current Price</th>
    					<th scope="col">MarketValue</th>
    					<th scope="col">Gain</th>
  					</tr>
  					<% for( int i=0; i<owned.size(); i++ ){ %>
  					<tr>
  					<%      
  							//initializing the color to represent the gain and loss
  							String color;
  							//getting the ownedstock object and corresponding stock object
  							Long sid = owned.get(i).getID();
  							Stock stock = ds.getStock(sid);
  							OwnedStock ownedStock = owned.get(i);
  							//getting the current price of the stock
  							double price = stock.getCurrentPrice();
  							//getting the price of the stock at the time of the purchase
  							double purchasePrice = owned.get(i).getPurchasePrice();
  							//getting the total market value of the stock owned
  							double mktVal = stock.getCurrentPrice()*owned.get(i).getQuantity();
  							//getting the bought value of the stock owned
  							double bghtVal = owned.get(i).getPurchasePrice() * owned.get(i).getQuantity();
  							//calculate the gain/loss and put it in the readable format
  							double gain = ((mktVal - bghtVal)/bghtVal)*100;
  							gain = Double.valueOf(twoDForm.format(gain));
  							mktVal = Double.valueOf(twoDForm.format(mktVal));
  							bghtVal = Double.valueOf(twoDForm.format(bghtVal));
  							price = Double.valueOf(twoDForm.format(price));
  							purchasePrice = Double.valueOf(twoDForm.format(purchasePrice));
  							//change the color of the content according to the gain/loss
  							if( gain > 0 ) color = "#00ff00";
    						else if( gain < 0 ) color = "#ff0000";
    						else color = "#000000";
    				%>
    			
    					<td>
    					<!-- sell button which allows for user to move to the profile page of the certain stock with sell button -->
    					<a href="sellstock_form.jsp?uid=<%=uid%>&ownedid=<%=ownedStock.getOwnedID()%>"><img src="media/sell.jpg"/></a></td>
    					<!-- the name of the celebrity which will link to the profile page of the celebrity with buy button -->
    					<td><a href="stockprofile.jsp?uid=<%=uid%>&stockid=<%=stock.getStockID() %>"><%=stock.getName() %></a></td>
    					<td><%=purchasePrice%></td>
    					<td><%=owned.get(i).getQuantity()%></td>
    					<td><%=price%></td>
    					<td><%=mktVal%> </td>
    					<td><div align="center"><span style="color:<%=color%>"><%=gain %>%</span></div></td>
  					</tr>
  					
  					<%	 } 	ds.close(); %>
  				</table>
    		</div>
			
    		<!-- end #mainContent -->
    	</div>
		
		<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
		<br class="clearfloat" />
   		
   		<div id="footer">
    		<p>Ranks will be here</p>
  		<!-- end #footer -->
  		</div>
  		
	<!-- end #container -->
	</div>
	
	</body>
</html>
